package com.chagee.channel.server.event.facade;

import cn.hutool.core.util.ArrayUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.chagee.base.constant.CommonErrorCode;
import com.chagee.base.utils.SoaResponse;
import com.chagee.base.utils.SoaUtil;
import com.chagee.channel.api.bo.FileInfoBO;
import com.chagee.channel.api.bo.oss.*;
import com.chagee.channel.api.service.IOssServiceApi;
import com.chagee.channel.api.utils.Jsr303Util;
import com.chagee.channel.common.exception.BizErrorCode;
import com.chagee.channel.common.exception.BizException;
import com.chagee.channel.server.event.service.OssService;
import com.chagee.channel.server.util.HttpUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import javax.validation.Validator;
import java.util.Set;

/**
 * @author: chang.jiawei
 * @date: 2024/03/14
 */
@Service
@Slf4j
public class OssServiceRpcImpl implements IOssServiceApi {

    @Resource
    private OssService ossService;
	@Resource
	private Validator validator;

    @Override
    public SoaResponse<String, Void> uploadV2(UploadOssRequestV2 request) {
        if(StrUtil.isEmpty(request.getUrl()) && ArrayUtil.isEmpty(request.getBytes())){
            throw new BizException(BizErrorCode.REQUEST_VALID_FAILED);
        }

        if(ArrayUtil.isNotEmpty(request.getBytes())){
            return SoaUtil.ok(ossService.upload(request.getBucketName(), request.getBytes(), request.getFileName()));
        }

        byte[] bytes = HttpUtils.downloadBytes(request.getUrl());
        return SoaUtil.ok(ossService.upload(request.getBucketName(), bytes, request.getFileName()));
    }

    public SoaResponse<String, Void> upload(byte[] pic, String fileName) {
        String url = ossService.upload(pic, fileName);
        return SoaUtil.ok(url);
    }

    public FileInfoBO uploadFsFile2Oss(String url, String fileName){
        log.info("入参URL：{}",url);
        log.info("入参fileName：{}",fileName);
        byte[] bytes = HttpUtils.downloadBytes(url);
        //上传文件到oss
//        IOssServiceApi ossServiceApi = SpringUtil.getBean(IOssServiceApi.class);
        SoaResponse<String, Void>  soaResponse=upload(bytes, fileName);
        log.info("ossServiceApiUpload: {}", JSONUtil.toJsonStr(soaResponse));
        FileInfoBO fileInfoBO = new FileInfoBO();
        try{
        String ossUrl = SoaUtil.unpack(soaResponse);
        fileInfoBO.setFileByte(bytes);
        fileInfoBO.setFileName(fileName);
        fileInfoBO.setFileType(fileName.substring(fileName.lastIndexOf(".")+1));
        fileInfoBO.setOssUrl(ossUrl);
        log.info("文件信息:{}", JSONUtil.toJsonStr(fileInfoBO));}
        catch (Exception e){
            log.info("上传失败");
            throw new BizException(BizErrorCode.FILE_UPLOAD_FAIL);
        }
        return fileInfoBO;
    }

    @Override
    public SoaResponse<String, Void> uploadByUrl(UploadOssRequest req) {
        String url = ossService.uploadByUrl(req);
        return SoaUtil.ok(url);
    }

    @Override
    public SoaResponse<SignedURLRep, Void> signedURL(SignedURLReq req) {
        return SoaUtil.ok(ossService.signedURL(req));
    }

	@Override
	public SoaResponse<DocumentConversionRep, Void> documentConversion(DocumentConversionReq req) {
		Set<ConstraintViolation<Object>> validate = validator.validate(req);
		String err = Jsr303Util.validate(validate);
		if (StringUtils.isNotBlank(err)) {
			return SoaUtil.error(CommonErrorCode.PARAMETER_IS_NULL.getErrorCode(), err, null);
		}
		return SoaUtil.ok(ossService.documentConversion(req));
	}
}
